/*
 * Created on 2008-06-08
 *
 * To change the template for this generated file go to
 * Window&gt;Preferences&gt;Java&gt;Code Generation&gt;Code and Comments
 */
package elka.isi.operator.writer;

import elka.isi.individual.AnimalIndividual;
import elka.isi.individual.RabbitIndividual;
import elka.isi.individual.RoeIndividual;
import elka.isi.util.RWUtils;
import org.evolvica.core.IIndividualSet;
import org.evolvica.core.ISetIterator;

import java.text.NumberFormat;
import java.util.Locale;

public class ScoresWriter extends EcosystemStateWriter {

	private NumberFormat df = NumberFormat.getNumberInstance(Locale.US);
	public ScoresWriter(String filename) {
		super(filename);
		// writer.append("RB_A" + DELIMITER + "RB_F" + DELIMITER + "RB_H" +
		// DELIMITER +
		// "RO_A" + DELIMITER + "RO_F" + DELIMITER + "RO_H" + DELIMITER +
		// "W_A" + DELIMITER + "W_F\n");
	}

	public void write(IIndividualSet set) {
		double averageRabbitFightScore = 0;
		double averageWolfFightScore = 0;
		double averageRoeFightScore = 0;
		double averageRabbitApetiteScore = 0;
		double averageRoeApetiteScore = 0;
		double averageWolfApetiteScore = 0;
		double averageRoeHarvestScore = 0;
		double averageRabbitHarvestScore = 0;
		double averageRabbitNutritionalScore = 0;
		double averageRabbitReproductionScore = 0;
		double averageRoeReproductionScore = 0;
		double averageRoeNutritionalScore = 0;
		double averageWolfReproductionScore = 0;

		double wolfCount = RWUtils.getWolves(set).size();
		double rabbitCount = RWUtils.getRabbits(set).size();
		double roeCount = RWUtils.getRoes(set).size();

		for (ISetIterator it = set.iterator(); it.hasNext();) {
			AnimalIndividual individual = (AnimalIndividual) it.next();
			switch (individual.getSpecies()) {
			case AnimalIndividual.RABBIT:
				averageRabbitFightScore += individual.getHuntingScore();
				averageRabbitHarvestScore += ((RabbitIndividual) individual)
						.getHarvestingScore();
				averageRabbitApetiteScore += individual.getAppetiteScore();
				averageRabbitReproductionScore += individual
						.getReproductionScore();
				averageRabbitNutritionalScore += ((RabbitIndividual) individual)
						.getNutritionalScore();
				break;

			case AnimalIndividual.ROE:
				averageRoeFightScore += individual.getHuntingScore();
				averageRoeHarvestScore += ((RoeIndividual) individual)
						.getHarvestingScore();
				averageRoeApetiteScore += individual.getAppetiteScore();
				averageRoeReproductionScore += individual
						.getReproductionScore();
				averageRoeNutritionalScore += ((RoeIndividual) individual)
						.getNutritionalScore();
				break;
			case AnimalIndividual.WOLF:
				averageWolfFightScore += individual.getHuntingScore();
				averageWolfApetiteScore += individual.getAppetiteScore();
				averageWolfReproductionScore += individual
						.getReproductionScore();
				break;
			default:
				break;
			}
		}
		if (rabbitCount != 0) {
			averageRabbitFightScore /= rabbitCount;
			averageRabbitHarvestScore /= rabbitCount;
			averageRabbitApetiteScore /= rabbitCount;
			averageRabbitReproductionScore /= rabbitCount;
			averageRabbitNutritionalScore /= rabbitCount;
		}
		if (roeCount != 0) {
			averageRoeFightScore /= roeCount;
			averageRoeApetiteScore /= roeCount;
			averageRoeHarvestScore /= roeCount;
			averageRoeReproductionScore /= roeCount;
			averageRoeNutritionalScore /= roeCount;
		}
		if (wolfCount != 0) {
			averageWolfApetiteScore /= wolfCount;
			averageWolfFightScore /= wolfCount;
		}
		
//		df.setGroupingUsed(false);
//		df.setMinimumFractionDigits(2);
//		df.setMaximumFractionDigits(2);

		writer.append(format(averageRabbitApetiteScore) + DELIMITER
				+ format(averageRabbitFightScore) + DELIMITER
				+ format(averageRabbitHarvestScore) + DELIMITER
				+ format(averageRoeApetiteScore) + DELIMITER
				+ format(averageRoeFightScore) + DELIMITER
				+ format(averageRoeHarvestScore) + DELIMITER
				+ format(averageWolfApetiteScore) + DELIMITER
				+ format(averageWolfFightScore) + DELIMITER
				+ format(averageRabbitNutritionalScore) + DELIMITER
				+ format(averageRabbitReproductionScore) + DELIMITER
				+ format(averageRoeNutritionalScore) + DELIMITER
				+ format(averageRoeReproductionScore) + DELIMITER
				+ format(averageWolfReproductionScore) + DELIMITER
				+ "\n");
	}
	private String format(double score){
		String ret = String.valueOf(score);
		int dot = ret.indexOf(",");
		if(dot != -1)
			ret = ret.substring(0, dot -1) + ret.substring(dot+1, ret.length()); 
		return ret;
	}

}
